

import { registerClass } from "../../../cfw/tools/class_decorator";
import { SDKDir, SDKLogicState, SDKState } from "../../SDKDefine";
import BaseAd from "../../core/base/BaseAd";
@registerClass('QQBannerAd')
export default class QQBannerAd extends BaseAd {
    hide(): void {
        throw new Error("Method not implemented.");
    }

    updateSize() {
        if (!this.ad) {
            return;
        }
        let winSize = this.sdk.getSystemInfoSync();
        if (this.style.width != 0 && this.style.height != 0 && this.ad) {
            switch (this.param.dir) {
                case SDKDir.BOTTOM_LEFT:
                    this.ad.style.top = (winSize.windowHeight - this.style.height);
                    this.ad.style.left = 0;
                    break;
                case SDKDir.BOTTOM_MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height);
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.RIGHT_MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height) / 2;
                    this.ad.style.left = winSize.windowWidth - this.style.width;
                    break;
                case SDKDir.MID:
                    this.ad.style.top = (winSize.windowHeight - this.style.height) / 2;
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.TOP_MID:
                    this.ad.style.top = 0;
                    this.ad.style.left = (winSize.windowWidth - this.style.width) / 2;
                    break;
                case SDKDir.XY:
                    this.ad.style.top = this.style.top//* winSize.windowHeight;
                    this.ad.style.left = this.style.left// * winSize.windowWidth;
                    break;
            }

        }
    }

    onRefresh() {

    }
    getStyle(): any {
        let winSize = this.sdk.getSystemInfoSync();
        let x = 0
        let y = 0
        switch (this.param.dir) {
            case SDKDir.BOTTOM_MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = winSize.windowHeight - this.style.height;
                break;
            case SDKDir.UP_MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = 0
                break;
            case SDKDir.MID:
                x = (winSize.windowWidth - this.style.width) / 2
                y = (winSize.windowHeight - this.style.height) / 2;
                break;
            case SDKDir.RIGHT_MID:
                x = (winSize.windowWidth - this.style.width)
                y = (winSize.windowHeight - this.style.height) / 2;
                break;
            case SDKDir.LEFT_TOP:
                x = 0
                y = 0
                break;
            case SDKDir.BOTTOM_LEFT:
                x = 0
                y = winSize.windowHeight - this.style.height;
                break;
            case SDKDir.XY:
                x = this.style.left// this.rx * winSize.windowWidth;
                y = this.style.top// this.ry * winSize.windowHeight;
                break;
            case SDKDir.WHITE:
                x = -this.style.width + 1
                y = -this.style.height + 1

                break;
        }
        return {
            left: x,
            top: y,
            width: this.style.width,
            height: this.style.height,
        }
    }
    //没有load方法
    load(): void {

    }
    show(): void {

        console.log(' QQBannerAd show 11111111111111111111')
        if (this.ad) {

            // this.setState(SDKState.open)
            this.ad.show().then(() => {
                this.setVisible(true)
                console.log(' QQBannerAd show true')

            }).catch(() => {
                console.log(' QQBannerAd show error')
            })

        }
    }
    onLoad() {
        this.setState(SDKState.loadSucess)
    }
    destroy() {
        if (this.ad) {
            this.ad.offLoad(this.getFunc(this.onLoad))
            this.ad.offError(this.getFunc(this.onError))
            this.ad.offResize(this.getFunc(this.onResize))
            this.ad = null;
        }
    }

    onError(err: any) {
        this.setState(SDKState.loadFail)
    }

    onResize(data: { width: number, height: number }) {
        // console.log(' banner onResize ', data)
        this.style.width = data.width;
        this.style.height = data.height;
        this.updateSize()
    }

    create() {
        if (!this.ad) {
            this.ad = this.sdk.createBannerAd({
                adUnitId: this.adUnitID,
                style: this.getStyle(),
                // adIntervals: 30,
            })
            this.createTime()
            this.ad.onLoad(this.getFunc(this.onLoad))
            this.ad.onError(this.getFunc(this.onError))
            this.ad.onResize(this.getFunc(this.onResize))
            // this.bannerAd.onRefresh(this.getFunc(this.onRefresh))
        }

    }
}
